package com.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.TableGenerator;

import com.core.protocols.IKVObject;
import com.core.utils.LocalizableString;

@Entity
public class ActionScript
        implements IKVObject {

	private Integer id;



	public ActionScript() {

		this.name = "doAction";
	}



	@Id
	@TableGenerator(allocationSize = 10, table = "Generators", pkColumnName = "generatorName", pkColumnValue = "generatorActionScript", name = "generatorActionScript")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "generatorActionScript")
	public Integer getId () {

		return id;
	}



	public void setId ( Integer id ) {

		this.id = id;
	}

	private String         actionScriptType;
	private String         name;
	private String         actionScriptText;
	private Set <KeyValue> params = new HashSet <KeyValue>();



	public String getName () {

		return name;
	}



	public void setName ( String name ) {

		this.name = name;
	}



	@Column(nullable = false)
	public String getActionScriptText () {

		return actionScriptText;
	}



	public void setActionScriptText ( String actionScriptText ) {

		this.actionScriptText = actionScriptText;
	}



	public void setActionScriptText ( String actionScriptTextFormat,
	                                  Object... arguments ) {

		String formattedScript = String.format(actionScriptTextFormat, arguments);
		this.actionScriptText = formattedScript;
	}



	public String getActionScriptType () {

		return actionScriptType;
	}



	public void setActionScriptType ( String actionScriptType ) {

		this.actionScriptType = actionScriptType;
	}



	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name = "actionscriptParams", joinColumns = {
		@JoinColumn(name = "ACTIONSCRIPT_ID")
	}, inverseJoinColumns = {
		@JoinColumn(name = "PARAM_ID")
	})
	public Set <KeyValue> getParams () {

		return params;
	}



	public void setParams ( Set <KeyValue> params ) {

		this.params = params;
	}



	/*
	 * IKVObject
	 */

	@Override
	public void assignTo ( IKVObject object ) {


	}



	@Override
	public KeyValue idValue () {

		String strIdValue = this.getId().toString();
		return new KeyValue(this.fieldNameAsId(), strIdValue, false);

	}



	@Override
	public String fieldNameAsId () {

		return "id";
	}



	@Override
	public KeyValue nameValue () {

		String strIdValue = this.getName().toString();
		return new KeyValue(this.fieldNameAsName(), strIdValue, false);

	}



	@Override
	public String fieldNameAsName () {

		return "name";
	}



	public String localizableDescription () {

		return LocalizableString.sharedInstance().localizable("entity.ActionScript.description");
	}
}
